home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-14.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  2KB  |  77 lines

  1. ;
  2. ; *** Listing 11-14 ***
  3. ;
  4. ; Finds the first occurrence of the letter 'z' in
  5. ; a zero-terminated string, using LODSW and checking
  6. ; 2 bytes per read.
  7. ;
  8.     jmp    Skip
  9. ;
  10. TestString    label    byte
  11.     db    'This is a test string that is '
  12.     db    'z'
  13.     db    'terminated with a zero byte...',0
  14. ;
  15. ; Finds the first occurrence of the specified byte in the
  16. ; specified zero-terminated string.
  17. ;
  18. ; Input:
  19. ;    AL = byte to find
  20. ;    DS:SI = zero-terminated string to search
  21. ;
  22. ; Output:
  23. ;    SI = pointer to first occurrence of byte in string,
  24. ;        or 0 if the byte wasn't found
  25. ;
  26. ; Registers altered: AX, BL, SI
  27. ;
  28. ; Direction flag cleared
  29. ;
  30. ; Note: Do not pass a string that starts at offset 0 (SI=0),
  31. ;    since a match on the first byte and failure to find
  32. ;    the byte would be indistinguishable.
  33. ;
  34. ; Note: Does not handle strings that are longer than 64K
  35. ;    bytes or cross segment boundaries.
  36. ;
  37. FindCharInString:
  38.     mov    bl,al    ;we'll need AX since that's the
  39.             ; only register LODSW can use
  40.     cld
  41. FindCharInStringLoop:
  42.     lodsw        ;get the next 2 string bytes
  43.     cmp    al,bl    ;is the first byte the byte we're
  44.             ; looking for?
  45.     jz    FindCharInStringDoneAdjust
  46.             ;yes, so we're done after we adjust
  47.             ; back to the first byte of the word
  48.     and    al,al    ;is the first byte the terminating
  49.             ; zero?
  50.     jz    FindCharInStringNoMatch ;yes, no match
  51.     cmp    ah,bl    ;is the second byte the byte we're
  52.             ; looking for?
  53.     jz    FindCharInStringDone
  54.             ;yes, so we're done
  55.     and    ah,ah    ;is the second byte the terminating
  56.             ; zero?
  57.     jnz    FindCharInStringLoop
  58.             ;no, so check the next 2 bytes
  59. FindCharInStringNoMatch:
  60.     sub    si,si    ;we didn't find a match, so return
  61.             ; 0 in SI
  62.     ret
  63. FindCharInStringDoneAdjust:
  64.     dec    si    ;adjust to the first byte of the
  65.             ; word we just read
  66. FindCharInStringDone:
  67.     dec    si    ;point back to the matching byte
  68.     ret
  69. ;
  70. Skip:
  71.     call    ZTimerOn
  72.     mov    al,'z'        ;byte value to find
  73.     mov    si,offset TestString
  74.                 ;string to search
  75.     call    FindCharInString ;search for the byte
  76.     call    ZTimerOff
  77.